package jglob;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
 * Main class.
 * @author serg
 */
public class Main {

    private static final String USAGE = "java -jar JGlob.jar";

    /**
     * Default protected constructor fro Stylecheck rule.
     */
    protected Main() {
        throw new UnsupportedOperationException("Constructor not Implemented.");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(final String[] args) {
        HelpFormatter frmt = new HelpFormatter();
        Options opts = new Options();

        Option help = new Option("h", "help", false, "Show this Help.");
        Option path = new Option("p", "path", true, "Search [PATH].");

        opts.addOption(help);
        opts.addOption(path);

        try {
            CommandLineParser parser = new PosixParser();
            CommandLine cmd = parser.parse(opts, args);

            if (cmd.hasOption('p')) {
                JGlob glob = new JGlob();
                for (String fn : glob.glob(cmd.getOptionValue("p"))) {
                    System.out.println(fn);
                }

                return;
            }

            frmt.printHelp(USAGE, opts);

        } catch (ParseException e) {
            frmt.printHelp(USAGE, opts);
            System.out.println("Parse Error: " + e.getMessage());

            return;
        }
    }
}
